#ifndef BINANCEC__SERVER_LOG_H
#define BINANCEC__SERVER_LOG_H

#include <iostream>
#include <mutex>
#include <fstream>
#include <stdarg.h>
#include <queue>
#include <string>
#include <semaphore.h>

constexpr int MAX_BUFFER_SIZE = 512;

enum LogState
{
    infoLog = 1,
    errorLog = 2
};

class Log
{
public:
    void init();
    static Log* getInstance()
    {
        static Log instance;
        return &instance;
    }

    void writeLog(LogState, const char*, ...);
    void logToFile();
    void flush();
    static void threadWriteLog()
    {
        Log::getInstance()->logToFile();
    };
    ~Log();

private:
    char buffer[MAX_BUFFER_SIZE];
    sem_t sem;
    std::fstream outFp;
    std::mutex m_mutex;
    std::queue<std::string> writeQueue;
    int maxWriteQueue = 30;
};

#define InfoLog(format, ...) {Log::getInstance()->writeLog(LogState::infoLog, format, ##__VA_ARGS__); Log::getInstance()->flush();}
#define ErrorLog(format, ...) {Log::getInstance()->writeLog(LogState::errorLog, format, ##__VA_ARGS__); Log::getInstance()->flush();}

#endif //BINANCEC__SERVER_LOG_H
